import request from '@/utils/request'

// 存款操作
export const deposit = async (accountId, amount) => {
    try {
        const response = await request({
            url: '/transactions/deposit',
            method: 'put',
            data: {  // 使用 data 发送请求体
                account_id: Number(accountId),  // 匹配后端的 getAccount_id()
                amount: Number(amount)
            }
        })
        // 返回完整的响应数据，包含 code、message 和 data
        return response
    } catch (error) {
        //alert("?????")
        console.error('存款操作失败:', error)
        return {
            code: -1,
            message: error.message || '存款操作失败',
            data: null
        }
    }
}

// 取款操作
export const withdraw = async (accountId, amount) => {
    try {
        // 打印请求参数，帮助调试
        console.log('取款请求参数:', {
            account_id: Number(accountId),
            amount: Number(amount)
        })

        const response = await request({
            url: '/transactions/withdraw',
            method: 'put',
            data: {
                account_id: Number(accountId),
                amount: Number(amount)
            }
        })
        return response
    } catch (error) {
        console.error('取款操作失败:', error)
        return {
            data: {
                code: -1,
                message: error.response?.data?.message || error.message || '取款操作失败',
                data: null
            }
        }
    }
}

// 转账操作
export const transfer = async (fromAccountId, toAccountId, amount) => {
    try {
        // 打印请求参数，帮助调试
        console.log('转账请求参数:', {
            account_id: Number(fromAccountId),
            dest_account_id: Number(toAccountId),
            amount: Number(amount)
        })

        const response = await request({
            url: '/transactions/transfer',
            method: 'put',
            data: {
                account_id: Number(fromAccountId),
                dest_account_id: Number(toAccountId),
                amount: Number(amount)
            }
        })
        return response
    } catch (error) {
        if (error.response?.data) {
            return error.response.data
        }
        return {
            code: -1,
            message: error.message || '转账操作失败',
            data: null
        }
    }
}